import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './Users.css';
import moment from 'moment';
import Factory from '../factory';
import numeral from 'numeral';
import * as datetime from '../../utils/datetime'
import dataEditCreate from '../addons/DataEdit'
import dataTableCreate from '../addons/DataTable'
import * as menuService from '../../services/menus';
import * as common from '../../utils/common';
import COMP_QTZB_DIVI_FHQK from './Comp/COMP_QTZB_DIVI_FHQK'

const daoName = "GJJSL";
const ButtonGroup = Button.Group;
const renderNumber = (num) => {
  let value = ''
  if (num) {
    value = numeral(num).format('0,0')
  }
  return value
};
const columns = [
  { title: 'JLID', dataIndex: 'JLID', key: 'JLID', width: 120, sorter: true, dispaly: false, filter: true },
  { title: '币种', width: 100, sorter: true, key: 'CurrCD', caption: "TBASECUCY.CYNM", render: data => data.TBASECUCY.CYNM, dispaly: true, filter: true },
  { title: '所属机构', width: 120, sorter: true, key: 'OID', caption: "Otion.OrganName", render: data => data.Otion.OrganName, dispaly: true, filter: true },
  { title: '发生日', width: 100, sorter: true, key: 'HappDT', caption: "HappDT", render: data => datetime.format(data.HappDT, "yyyy-MM-dd"), dispaly: true, filter: true, canEdit: true },
  { title: '到期日', width: 100, sorter: true, key: 'MatureDT', caption: "MatureDT", render: data => datetime.format(data.MatureDT, "yyyy-MM-dd"), dispaly: true, filter: true, canEdit: true },
  { title: '客户名称', width: 220, sorter: true, nowrap: true, key: 'CustnaME', dispaly: true, filter: true },
  { title: '手续费收入', className: 'column-money', caption: "CommfeeincoME", render: data => common.formatNumber(data.CommfeeincoME), key: 'CommfeeincoME', className: 'column-money', width: 140, sorter: true, dispaly: true, filter: true },
  { title: '发生金额', className: 'column-money', caption: "Amt", render: data => common.formatNumber(data.Amt), key: 'Amt', width: 120, sorter: true, className: 'column-money', dispaly: true, filter: true },
  { title: '当前余额', className: 'column-money', caption: "CurrbAL", render: data => common.formatNumber(data.CurrbAL), key: 'CurrbAL', width: 120, sorter: true, dispaly: true, filter: true },
  { title: '保证金金额', className: 'column-money', caption: "MarginAmt", render: data => common.formatNumber(data.MarginAmt), key: 'MarginAmt', width: 140, sorter: true, dispaly: true, filter: true },
  { title: '保证金币种', dataIndex: 'MarginCurr', key: 'MarginCurr', width: 140, sorter: true, dispaly: true, filter: true },
  { title: '组织机构代码', dataIndex: 'OrgorgCd', key: 'OrgorgCd', width: 140, sorter: true, dispaly: true, filter: true },
  { title: '科目号', dataIndex: 'Glunm', key: 'Glunm', width: 120, sorter: true, dispaly: true, filter: true },
  { title: '科目号2', dataIndex: 'Glunm2', key: 'Glunm2', width: 120, sorter: true, dispaly: true, filter: true },
  { title: '当前余额2', className: 'column-money', caption: "Amtdistcntrmb", render: data => common.formatNumber(data.Amtdistcntrmb), key: 'Glunmbal2', width: 170, sorter: true, dispaly: true, filter: true },
  { title: '发生金额折人民币', className: 'column-money', caption: "Amtdistcntrmb", render: data => common.formatNumber(data.Amtdistcntrmb), key: 'Amtdistcntrmb', width: 170, sorter: true, dispaly: true, filter: true },
  { title: '当前余额折人民币', className: 'column-money', caption: "Currbaldiscntrmb", render: data => common.formatNumber(data.Currbaldiscntrmb), key: 'Currbaldiscntrmb', width: 170, sorter: true, dispaly: true, filter: true },
  { title: '手续费收入折人民币', className: 'column-money', caption: "Commfeeincomediscntrmb", render: data => common.formatNumber(data.Commfeeincomediscntrmb), key: 'Commfeeincomediscntrmb', width: 180, sorter: true, dispaly: true, filter: true },
  { title: '当前余额2折人民币', className: 'column-money', caption: "Currbal2discntrmb", render: data => common.formatNumber(data.Currbal2discntrmb), key: 'Currbal2discntrmb', width: 170, sorter: true, dispaly: true, filter: true },
  { title: '保证金金额折人民币', className: 'column-money', caption: "Marginamtdiscntrmb", render: data => common.formatNumber(data.Marginamtdiscntrmb), key: 'Marginamtdiscntrmb', width: 180, sorter: true, dispaly: true, filter: true },
  // { title: '客户经理1', width: 100, sorter: false, dataIndex: 'YG1', key: 'YG1', dispaly: true, filter: false },
  // { title: '分配比例1(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB1', render: data => renderNumber(data.ZB1), key: 'ZB1', dispaly: true, filter: false },
  // { title: '客户经理2', width: 100, sorter: false, dataIndex: 'YG2', key: 'YG2', dispaly: true, filter: false },
  // { title: '分配比例2(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB2', render: data => renderNumber(data.ZB2), key: 'ZB2', dispaly: true, filter: false },
  // { title: '客户经理3', width: 100, sorter: false, dataIndex: 'YG3', key: 'YG3', dispaly: true, filter: false },
  // { title: '分配比例3(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB3', render: data => renderNumber(data.ZB3), key: 'ZB3', dispaly: true, filter: false },
  // { title: '客户经理4', width: 100, sorter: false, dataIndex: 'YG4', key: 'YG4', dispaly: true, filter: false },
  // { title: '分配比例4(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB4', render: data => renderNumber(data.ZB4), key: 'ZB4', dispaly: true, filter: false },
  // { title: '客户经理5', width: 100, sorter: false, dataIndex: 'YG5', key: 'YG5', dispaly: true, filter: false },
  // { title: '分配比例5(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB5', render: data => renderNumber(data.ZB5), key: 'ZB5', dispaly: true, filter: false },
];
const controls = [
  { name: 'CommfeeincoME', required: true, type: 'alphanum', min: 6, max: 6, label: '手续费收入', grid: 12 / 24 },
  { name: 'JLID', required: true, type: 'alphanum', min: 6, max: 6, label: 'JLID', grid: 12 / 24 },
  {
    name: "CurrCD", label: "币种", type: "select", grid: 12 / 24, required: true,
    dataType: "string", fetch: { daoName: "TBASECUCY", optionTpl: 'CYNM', valueTpl: "CYNO" }
  },
  {
    name: "OID", label: "所属机构", type: "select", grid: 12 / 24, required: true,
    dataType: "string", fetch: { daoName: "Otion", optionTpl: 'OrganName', valueTpl: "ID" }
  },
  { name: "HappDT", label: "发生日", type: "date", grid: 12 / 24, dataType: "date" },
  { name: "MatureDT", label: "到期日", type: "date", grid: 12 / 24, dataType: "date" },
  { name: 'CustnaME', required: true, type: 'text', label: '客户名称', min: 2, max: 30, grid: 12 / 24 },
  { name: 'Amt', required: true, type: 'alphanum', min: 6, max: 6, label: '发生金额', grid: 12 / 24 },
  { name: 'CurrbAL', required: true, type: 'alphanum', min: 6, max: 6, label: '当前余额', grid: 12 / 24 },
  { name: 'MarginAmt', required: true, type: 'alphanum', min: 6, max: 6, label: '保证金金额', grid: 12 / 24 },
  { name: 'MarginCurr', required: true, type: 'alphanum', min: 6, max: 6, label: '保证金币种', grid: 12 / 24 },
  { name: 'OrgorgCd', required: true, type: 'alphanum', min: 6, max: 6, label: '手续费收入', grid: 12 / 24 },
  { name: 'Glunm', required: true, type: 'alphanum', min: 6, max: 6, label: '组织机构代码', grid: 12 / 24 },
  { name: 'Glunm2', required: true, type: 'alphanum', min: 6, max: 6, label: '科目号', grid: 12 / 24 },
  { name: 'Glunmbal2', required: true, type: 'alphanum', min: 6, max: 6, label: '科目号2', grid: 12 / 24 },
  { name: 'Amtdistcntrmb', required: true, type: 'alphanum', min: 6, max: 6, label: '发生金额折人民币', grid: 12 / 24 },
  { name: 'Currbaldiscntrmb', required: true, type: 'alphanum', min: 6, max: 6, label: '当前余额折人民币', grid: 12 / 24 },
  { name: 'Commfeeincomediscntrmb', required: true, type: 'alphanum', min: 6, max: 6, label: '手续费收入折人民币', grid: 12 / 24 },
  { name: 'Currbal2discntrmb', required: true, type: 'alphanum', min: 6, max: 6, label: '当前余额2折人民币', grid: 12 / 24 },
  { name: 'Marginamtdiscntrmb', required: true, type: 'alphanum', min: 6, max: 6, label: '保证金金额折人民币', grid: 12 / 24 },
]
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
class GJJSLS extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "JLID", permID: this.props.permID }

    });
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 20 }
    });
    this.loadOptionData();
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  showModelHandler(isAdd) {
    let key = null
    if (!isAdd) {
      const len = this.props.selectedRowKeys.length;
      if (len === 0) {
        message.info('请选择要编辑的行，如果选择了多行，则使用最后一次选择！');
        return;
      }
      key = this.props.selectedRowKeys[len - 1]
    }
    this.props.dispatch({
      type: `${daoName}/openEdit`,
      payload: { key }
    });
  }
  hideEditHandler() {
    if (this.state.dataChange) {
      this.props.dispatch({
        type: `${daoName}/reload`,
        payload: { daoName },
      });
      this.state.dataChange = false;
    }
    this.props.dispatch({
      type: `${daoName}/hideEdit`
    });
  };
  render() {
    let { showEdit, record, permID, popedomTypes } = this.props
    const pTypes = popedomTypes[permID]
    let data = []
    if (pTypes.indexOf('6') !== -1) {
      let button = { value: '2', label: '业绩认定', onClick: this.showModelHandler.bind(this, false) }
      data.push(button)
    }
    const controlsFilter = [
      { name: 'CX', required: false, type: 'button-group', data: data, grid: 5 / 24 },
    ];
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
            {/* <EditFrom ref="findForm" daoName={daoName} controls={controlsFilter} clearItemMargin={true} selectFirst={false} /> */}
          </div>
          <COMP_QTZB_DIVI_FHQK ref="divi" jlid={record.JLID} ywid={9} title={"业绩分配"} showEdit={showEdit} parentModel={daoName} />
          <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
        </div>
      </div>
    )
  }
}
function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  const { userID, orgID, pOrgID, popedomTypes } = state.menus;
  return {
    userID,
    orgID,
    pOrgID,
    popedomTypes,
    showEdit,
    selectedRowKeys,
    keyName,
    record
  }
}
GJJSLS = connect(mapStateToProps)(GJJSLS)
export default GJJSLS;
let render = (props) => React.createElement(GJJSLS, props);
let f = new Factory()
f.set("GJJSLS", render)
